import json
import time
import urllib

import requests
from _pytest import logging
from selenium.webdriver import Keys, ActionChains

from seleniumbase import BaseCase

# 命令行后面可以输入--reuse-session保持浏览器开启
# 方法二用断点import pdb,pdb.set_trace(),c+enter来继续执行

class TestUtils(BaseCase):

    # 该方法用于等待元素出现后再进行点击操作,然后点击关闭页面
    def click_when_visible_and_close(self, selector):
        self.wait_for_element(selector, timeout=15)
        time.sleep(1)
        self.click(selector)
        self.check_status(self)
        element = '#tags-view-container > div.tags-view-wrapper > span.tags-view-item.router-link-exact-active.router-link-active.active > span.tag-icon-close'
        time.sleep(1)
        self.wait_for_element(element, timeout=15)
        self.click(element)

        # 该方法用于等待元素出现后再进行点击操作
    def click_when_visible(self, selector):
        self.wait_for_element_visible(selector, timeout=25)
        self.click(selector)
        self.check_status(self)


    # 该方法用于等待元素出现后再进行输入操作
    def send_when_visible(self, selector, text):
        self.wait_for_element_visible(selector, timeout=15)
        self.type(selector, text)

    def clear_when_visible(self, selector):
        self.wait_for_element_visible(selector, timeout=15)
        self.clear(selector)

    # 该方法用于等待元素出现后再进行输入方向操作
    def sendkey_when_visible(self, selector, text):
        self.wait_for_element_visible(selector, timeout=15)
        if text == "enter":
            self.send_keys(selector, Keys.ENTER)
        if text == "down":
            self.send_keys(selector, Keys.DOWN)
        if text == "up":
            self.send_keys(selector, Keys.UP)
        if text == "left":
            self.send_keys(selector, Keys.LEFT)
        if text == "right":
            self.send_keys(selector, Keys.RIGHT)

    # 直接操作键盘按钮的TAB
    def actions_tab(self, text):
        actions = ActionChains(self.driver)
        while text > 0:
            actions.send_keys(Keys.TAB).perform()
            text -= 1

    def actions_down(self, text):
        actions = ActionChains(self.driver)
        while text > 0:
            actions.send_keys(Keys.DOWN).perform()
            text -= 1

    def actions_right(self, text):
        actions = ActionChains(self.driver)
        while text > 0:
            actions.send_keys(Keys.RIGHT).perform()
            text -= 1
    def actions_enter(self, text):
        actions = ActionChains(self.driver)
        while text > 0:
            actions.send_keys(Keys.ENTER).perform()
            text -= 1

    def actions_send(self, text):
        actions = ActionChains(self.driver)
        actions.send_keys(text).perform()

    def actions_space(self, text):
        actions = ActionChains(self.driver)
        while text > 0:
            actions.send_keys(Keys.SPACE).perform()
            text -= 1


    # 该方法用于解析cookie字符串并添加到浏览器中
    def cookie_adder(self, cookie_string):
        cookie_elements = cookie_string.split("; ")
        for element in cookie_elements:
            name, value = element.split("=", 1)
            self.driver.add_cookie({'name': name.strip(), 'value': value.strip()})


    # 该方法用于检测页面状态与返回状态码
    def check_status(self, url):
        current_url = self.get_current_url()  # 获取跳转后的页面 URL
        print(current_url)
        response = requests.get(current_url)  # 检查页面的状态
        print(response)

        if response.status_code != 200:
            # 这是 HTTP 状态码的断言
            time.sleep(1)
            self.save_screenshot("D://zj_word//bug_phone//error_screenshot.png")  # 保存截图
            logging.error(f'错误页面 {current_url} 返回状态码: {response.status_code}')  # 打印错误日志
            assert False, f'错误页面 {current_url} 返回状态码: {response.status_code}'

        # Check if the response is JSON
        if 'application/json' in response.headers['Content-Type']:
            response_json = json.loads(response.text)
            # 这是业务状态码的断言
            if 'code' in response_json and response_json['code'] != 200:  # 假设正确的业务状态码是200
                time.sleep(1)
                self.save_screenshot("D://zj_word//bug_phone//error_screenshot.png")  # 保存截图
                logging.error(f'错误页面 {current_url} 返回业务状态码: {response_json["code"]}')  # 打印错误日志
                assert False, f'错误页面 {current_url} 返回业务状态码: {response_json["code"]}'
        else:
            print("http.code:200")

